package com.prs.crm.action.oa.bookmanager;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.hr.Resume;
import com.prs.crm.domain.oa.bookmanager.BookRegister;
import com.prs.crm.domain.oa.bookmanager.BorrowRegister;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.approval.impl.ShenQingTypeServiceImpl;
import com.prs.crm.service.hr.ResumeService;
import com.prs.crm.service.oa.bookmanager.BookRegisterService;
import com.prs.crm.service.oa.bookmanager.BorrowRegisterService;

public class BorrowRegisterAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private BorrowRegister borrowRegister;
	private BorrowRegisterService borrowRegisterService;
	private Integer[] ids;
	private List<BookRegister> bookRegister;
	private BookRegisterService bookRegisterService;
	private PaginationSupport pagination;
	private Collection<Resume> borrowPerson;
	private ResumeService resumeService;
	private Integer number;
	private File upload;
	private String uploadFileName;
	private Application application;
	private ApplicationService ApplicationService;
	
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public ApplicationService getApplicationService() {
		return ApplicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}
	
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}
	public Collection<Resume> getBorrowPerson() {
		return borrowPerson;
	}
	public void setBorrowPerson(Collection<Resume> borrowPerson) {
		this.borrowPerson = borrowPerson;
	}
	public ResumeService getResumeService() {
		return resumeService;
	}
	public void setResumeService(ResumeService resumeService) {
		this.resumeService = resumeService;
	}
	public BorrowRegister getBorrowRegister() {
		return borrowRegister;
	}
	public void setBorrowRegister(BorrowRegister borrowRegister) {
		this.borrowRegister = borrowRegister;
	}
	public BorrowRegisterService getBorrowRegisterService() {
		return borrowRegisterService;
	}
	public void setBorrowRegisterService(BorrowRegisterService borrowRegisterService) {
		this.borrowRegisterService = borrowRegisterService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List<BookRegister> getBookRegister() {
		return bookRegister;
	}
	public void setBookRegister(List<BookRegister> bookRegister) {
		this.bookRegister = bookRegister;
	}
	public BookRegisterService getBookRegisterService() {
		return bookRegisterService;
	}
	public void setBookRegisterService(BookRegisterService bookRegisterService) {
		this.bookRegisterService = bookRegisterService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	
	@Action("listBorrowRegister")
	public String listBorrowRegister(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(BorrowRegister.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBorrowRegisterService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editBorrowRegister")
	public String editBorrowRegister(){
		this.borrowPerson=this.getResumeService().getAllResume();
		this.bookRegister=this.getBookRegisterService().getAllBookRegister();
		return SUCCESS;
	}
	@Action("viewBorrowRegister")
	public String viewBorrowRegister(){
		this.borrowPerson=this.getResumeService().getAllResume();
		this.bookRegister=this.getBookRegisterService().getAllBookRegister();
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@Action(value = "saveBorrowRegister", results = {
			@Result(name = "success", type = "redirect", location = "listBorrowRegister"),
			@Result(name = "error", type = "chain", location = "editBorrowRegister") })
	public String saveBorrowRegister(){
		if (upload!=null) {
			this.borrowRegister.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		this.getBorrowRegisterService().save(borrowRegister);
		BookRegister br=this.getBookRegisterService().getBookRegisterByName(borrowRegister.getBookRegister().getBookName());
		if (br!=null) {
			Integer nums=br.getNumber()-borrowRegister.getNum();
			if (nums<0) {
				this.addActionError(br.getBookNo()+"此书数量不足，操作失败");
				this.getBorrowRegisterService().remove(borrowRegister);
				return ERROR;
			}
			br.setNumber(nums);
			this.getBookRegisterService().submit(br);
		}
		
		application = new Application();
		application.setType("一般");
		application.setAppName("[一般]"+"借阅登记");
		application.setSqType(2);
		application.setSpr(0);
		application.setPerson(borrowRegister.getPerson());
		application.setBorrowRegister(borrowRegister);
		application.setResign("借阅登记");
		this.getApplicationService().save(application);
		return SUCCESS;
	}
	@Action(value = "removeBorrowRegister", results = {
			@Result(name = "success", type = "redirect", location = "listBorrowRegister"),
			@Result(name = "error", type = "chain", location = "listBorrowRegister") })
	public String removeBorrowRegister(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getBorrowRegisterService().batchRemove(ids);
		return SUCCESS;
	}
}
